<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save" @settlement="settlement" @allsingleTransfer="allsingleTransfer"></v-tableTitle>
      <div class="detailBox">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from '@/mixins/BaseMixin'
import skipMixin from '@/mixins/SkipMixin'
import config from '../Config'
import tableTitle from 'commons/TableTitle'
import detail from '../details/Detail'
import detailList from '../detailsList/DetailList'

export default {
  name: '',
  mixins: [baseMixin, skipMixin, config],
  data() {
    return {
      newFlag: false,
      routeData: {}
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.save'),
          certigier:
            this.saveStatus || !this.btnPermNameList.includes('WM06050106'),
          invalid: 'save'
        },
        {
          label: this.$t('work.settlement'),
          certigier:
            this.settlementStatus ||
            !this.btnPermNameList.includes('WM06050107'),
          invalid: 'settlement'
        },
        {
          label: this.$t('work.allsingleTransfer'),
          certigier:
            this.deleteTransferStatus ||
            !this.btnPermNameList.includes('WM06050108'),
          invalid: 'allsingleTransfer'
        },
        {
          label: this.$t('common.return'),
          certigier: !this.btnPermNameList.includes('WM06050109'),
          invalid: 'cancel'
        }
      ]
    }
  },
  created() {},
  methods: {
    async allsingleTransfer() {
      try {
        let res = await this.$api.post(`/allsingleTransfer`, this.routeData)
        
          this.$message.success(res.returnMgr)
        
      } catch (e) {
        this.$message.error(e)
      }
    },
    async settlement() {
      try {
        let data = []
        data[0] = this.routeData
        let res = await this.$api.post(`/settlementTransfer`, data)
        
          this.$message.success(res.returnMgr)
        
      } catch (e) {
        this.$message.error(e)
      }
    },
    //保存按钮
    save() {
      console.log(this.$refs);
      this.$refs.detail.$children[0].$refs.input.$refs['from'].validate(
        valid => {
          if (valid) {
            let data = {}
            let detail = this.$refs.detail.detailData
            let detailList = this.$refs.detailList.detailData
            if (this.$refs.detailList.detailsFlag) {
              this.$refs.detailList.$children[2].$refs.input.$refs[
                'from'
              ].validate(flag => {
                if (flag) {
                  data = {
                    ...detail,
                    transferdetail: [
                      {
                        ...detailList,
                        transferkey: detail.transferkey
                      }
                    ]
                  }
                  this.http(data)
                }
              })
            } else {
              data = {
                ...detail
              }
              this.http(data)
            }
          }
        }
      )
    },
    async http(data) {
      try {
        let res = await this.$api.post('/saveTransfer', data)
        
          this.$message.success(res.returnMgr)
          if (this.$store.state.newFlag) {
            this.cancel()
          } else if (this.$refs.detailList.detailsFlag) {
            this.$refs.detailList.detailsFlag = false
            this.$refs.detailList.detailNav = '1'
          } else {
            this.cancel()
          }
        
      } catch (e) {
        this.$message.error(e)
      }
    },
    //弹框取消
    cancel() {
      let routerName = JSON.parse(sessionStorage.getItem('routerName'))
      let num = -1
      if (routerName) {
        num = routerName.indexOf('transfer')
      }
      if (num === -1) {
        this.$router.push({ name: 'transfer' })
      } else {
        num = num - 0
        routerName.splice(num, 1)
        sessionStorage.setItem('routerName', JSON.stringify(routerName))
        this.$router.push({ name: 'home1' })
      }
    }
  },
  mounted() {
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip
      this.routeData = data.routeData
      this.newFlag = data.newFlag
    } else {
      this.routeData = this.$store.state.routeData
      this.newFlag = this.$store.state.newFlag
    }
  },
  watch: {},
  components: {
    'v-tableTitle': tableTitle,
    'v-detail': detail,
    'v-detailList': detailList
  },
  beforeDestroy() {
    let routerName = JSON.parse(sessionStorage.getItem('routerName'))
    let num = -1
    if (routerName) {
      num = routerName.indexOf('transfer')
    }
    if (num === -1) {
    } else {
      num = num - 0
      routerName.splice(num, 1)
      sessionStorage.setItem('routerName', JSON.stringify(routerName))
    }
  }
}
</script>

<style scoped>
</style>
